package com.ztq.test.system.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

/**
 * @author: ztq
 * @date: 2016-1-8 下午05:08:48
 * @description: 工具类CommonUtils
 * @version:1.0
 */
public class CommonUtils {
	/**
	 * 功能描述：获取唯一流水号
	 * @return
	 */
	public static String getUUID() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}
	
	/**
	 * 将str从响应端发送到请求端
	 */
	public static void responseWrite(String str) {
		try {
			HttpServletResponse reposne = ServletActionContext.getResponse();
			reposne.setCharacterEncoding("UTF-8");
			reposne.getWriter().print(str);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 功能描述：实现将数据转换为MD5字符串
	 * @param str_val
	 * @return String
	 */
	public static String ToMD5(String str_val) {
		if (str_val != null && !str_val.equalsIgnoreCase("")) {
			MessageDigest md = null;
			String md5_val = "";
			try {
				md = MessageDigest.getInstance("MD5");
				sun.misc.BASE64Encoder baseEncoder = new sun.misc.BASE64Encoder();
				md5_val = baseEncoder.encode(md.digest(str_val
						.getBytes("UTF-8")));
			} catch (NoSuchAlgorithmException e) {
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			return md5_val;
		} else {
			return "";
		}
	}
}